//
//  YTDriverManagerViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/7/18.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTDriverManagerViewController.h"
#import "YTDriverManagerViewCell.h"
#import "YTDriverManagerViewModel.h"
#import "YTTeamDriverModel.h"
#import "YTDriverDetailViewController.h"

@interface YTDriverManagerViewController ()
@property (strong, nonatomic) YTDriverManagerViewModel *viewModel;
@end

@implementation YTDriverManagerViewController

- (void)initialize {
    [super initialize];
    _viewModel = [[YTDriverManagerViewModel alloc] init];
    @weakify(self);
    [self addLoadDataControl:^{
        @strongify(self);
        [self.viewModel.loadMoreDataCommand execute:nil];
    }];
    
    [[[self rac_signalForSelector:@selector(viewWillAppear:)]
      skip:1]
      subscribeNext:^(id x) {
        @strongify(self);
        [self.viewModel.requestRemoteDataCommand execute:nil];
    }];
}

- (void)bindViewModel {
    [super bindViewModel];
    @weakify(self);
    // 刷新
    [[_viewModel.requestRemoteDataCommand.executionSignals switchToLatest]
     subscribeNext:^(id x) {
        @strongify(self);
         [self.tableView reloadData];
    }];
    [_viewModel.requestRemoteDataCommand.executing subscribeNext:[self yt_showHudWithTips:nil]];
    // 加载更多
    [[_viewModel.loadMoreDataCommand.executionSignals switchToLatest]
     subscribeNext:^(NSArray<YTTeamDriverModel *> *data) {
        @strongify(self);
        if (data.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView reloadData];
    }];
    // 删除司导
    [[_viewModel.deleteDriverCommand.executionSignals switchToLatest]
     subscribeNext:^(id x) {
        @strongify(self);
        [self.viewModel.requestRemoteDataCommand execute:nil];
    }];
    
    [self.viewModel.requestRemoteDataCommand execute:nil];
}

#pragma mark - UITableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _viewModel.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YTDriverManagerViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YTDriverManagerCell" forIndexPath:indexPath];
    if (indexPath.row <= _viewModel.dataArray.count) {
        cell.model = _viewModel.dataArray[indexPath.row];
    }
    @weakify(self);
    cell.deleteAction = ^(NSString *driverId) {
        @strongify(self);
        [self deleteDriver:driverId];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row <= _viewModel.dataArray.count) {
        [self performSegueWithIdentifier:@"YTDriverDetail" sender:_viewModel.dataArray[indexPath.row].driverId];
    }
}

#pragma mark - 删除司机

- (void)deleteDriver:(NSString *)driver_id {
    [UIAlertView bk_showAlertViewWithTitle:@"您确定删除该司机吗？" message:nil
                         cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"]
                                   handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
        if (buttonIndex == 1) {
            [self.viewModel.deleteDriverCommand execute:driver_id];
        }
    }];
}

#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"YTDriverDetail"]) {
        YTDriverDetailViewController *detail = segue.destinationViewController;
        detail.driver_id = sender;
    }
}

@end
